/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.driving.core.base.BaseServiceImpl;
import com.driving.core.entity.Video;
import com.driving.core.entity.VideoCategory;
import com.driving.core.mapper.IVideoCategoryMapper;
import com.driving.core.mapper.IVideoMapper;
import com.driving.core.service.IVideoService;

import lombok.RequiredArgsConstructor;

/**
 * @author SAGITTAR
 * @since 2017
 */
@RequiredArgsConstructor
@Service(value = "videoService")
public class VideoServiceImpl extends BaseServiceImpl<IVideoMapper, Video> implements IVideoService {

    @Value(value = "${video.path}")
    private String videoPath;

    private final IVideoCategoryMapper videoCategoryMapper;

    @Override
    public Video selectByName(String name) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return getOne(wrapper);
    }

    @Override
    public List<Map<String, String>> selectListBySubjectIdOrCategoryId(int type, int subIdOrCatId, boolean isLogin) {
        List<Video> videoList = null;
        if (type == 1) {
            videoList = baseMapper.selectListBySubjectId(subIdOrCatId);
        }
        if (type == 2) {
            QueryWrapper<Video> wrapper = new QueryWrapper<>();
            wrapper.eq("category_id", subIdOrCatId);
            wrapper.eq(!isLogin, "source", 0);
            videoList = list(wrapper);
        }
        if (type != 1 && type != 2) {
            videoList = new ArrayList<>(1);
        }
        return videoList.stream().map(v -> {
            Map<String, String> video = new HashMap<>(8);
            video.put("id", String.valueOf(v.getId()));
            video.put("name", v.getName());
            video.put("file", StringUtils.join(videoPath, v.getFile()));
            video.put("description", v.getDescription());
            return video;
        }).collect(Collectors.toList());
    }

    @Override
    public List<VideoCategory> selectListBySubjectId(int subjectId) {
        QueryWrapper<VideoCategory> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("subject_id", subjectId);
        return videoCategoryMapper.selectList(categoryWrapper);
    }
}
